package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;
    //列表查询
    @GetMapping()
    public Result list(StudentQueryParam studentQueryParam) {
        log.info("查询学生列表");
        PageResult<Student> pageResult = studentService.list(studentQueryParam);
        return Result.success(pageResult);
    }

    //删除学生  注意{ids}要用@PathVariable注解
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids)
    {
        log.info("删除学生");
        studentService.deleteById(ids);
        return Result.success();
    }

    //添加
    @PostMapping()
    public Result add(@RequestBody Student student)
    {
        log.info("添加学生");
        studentService.add(student);
        return Result.success();
    }

    //查询
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id)
    {
        log.info("查询学生");
        Student student=studentService.get(id);
        return Result.success(student);
    }

    //修改
    @PutMapping()
    public Result update(@RequestBody Student student)
    {
        log.info("修改学生");
        studentService.update(student);
        return Result.success();
    }

    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id,@PathVariable Integer score)
    {
        log.info("违纪处理");
        studentService.violation(id,score);
        return Result.success();
    }
}
